<template>
  <!-- 用户管理页面组件 -->
  <div class="RootBox">
    <!-- 面包屑导航 -->
    <div class="wanjun">
      <div class="mbnav">
        <div class="mbnavsontwo">
          <!-- 左侧图标 -->
          <el-row class="iconlist">
            <span class="mobile">
              <el-button class="addUser" size="mini" icon="iconfont icon-dingbudaohang-zhangh btnicon" @click="addUser"
                >新建</el-button
              >
            </span>
            <span class="mobile">
              <el-button size="mini" class="Refresh" icon="iconfont icon-ico_reset btnicon" @click="RefreshUser"
                >刷新</el-button
              >
            </span>

            <span class="mobile">
              <el-button size="mini" class="Refresh" icon="iconfont " @click="downloadFile">下载模板</el-button>
            </span>
            <span class="mobile">
              <el-upload action="*" :auto-upload="false" :show-file-list="false" :on-change="selectFileChange">
                <el-button size="mini" type="primary" icon="iconfont ">导入文件</el-button>
              </el-upload>
            </span>
            <span class="mobile">
              <el-upload class="upload-demo" :action="actionUrl" :data="{ creator: creator }">
                <el-button size="mini" class="addUser" icon="iconfont icon-ico_reset btnicon">导入招标信息</el-button>
              </el-upload>
            </span>
          </el-row>
          <div class="mobile searchfather">
            <el-input
              placeholder="通过标题查询"
              size="medium"
              clearable
              v-model="userName"
              class="input-with-select"
            ></el-input>
            <!-- <el-input
              placeholder="通过标题查询"
              clearable
              v-model="linkUserName"
              class="input-with-select"
            ></el-input> -->
            <el-select clearable v-model="linkUserName" size="medium" placeholder="通过发布状态搜索">
              <el-option
                v-for="(item, index) in isEdstatusList"
                :key="index"
                :label="item.name"
                :value="item.value"
              ></el-option>
            </el-select>
            <el-date-picker
              v-model="datefb"
              type="daterange"
              size="medium"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>

            <el-button class="search" size="mini" type="primary" @click="serachResult">搜索</el-button>
            <el-button class="search" size="mini" type="primary" @click="resetResult">重置</el-button>
          </div>
        </div>
      </div>
    </div>
    <!-- 树状图加表格 -->
    <div class="tabbleList">
      <!-- 表单结构图 -->
      <div class="fatherform">
        <div class="tableFather">
          <el-table
            v-loading="loading"
            height="540"
            v-if="tableDatarow"
            border=""
            :data="tableDatarow"
            @selection-change="tablerowchange"
            stripe
            style="width: 100%"
            ref="multipleTable"
            size="mini"
            :header-cell-style="{ background: '#eff3f8', color: '#606266' }"
          >
            <el-table-column prop="noticeTitle" label="标题">
              <template slot-scope="{ row }">
                <div>
                  <span class="linkTitle" @click="openNoticeDialog(row)">{{ row.noticeTitle }}</span>
                </div>
              </template>
            </el-table-column>

            <el-table-column prop="noticeType" :formatter="formatter" label="类型"></el-table-column>
            <el-table-column prop="status" label="发布状态">
              <template slot-scope="scope">
                <div>
                  <el-switch
                    v-model="scope.row.status"
                    active-text="已发布"
                    :active-value="0"
                    :inactive-value="1"
                    @change="changeHandler(scope.row)"
                    inactive-text="未发布"
                  >
                  </el-switch>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="updateDatetime" label="发布时间"></el-table-column>
            <el-table-column label="操作" width="330">
              <template slot-scope="scope">
                <div style="text-align: left; display: flex">
                  <el-button v-if="isShowFalg" size="mini" class="updateUser" @click="updateUser(scope.row)"
                    >编辑</el-button
                  >
                  <el-button v-if="isShowFalg" size="mini" type="danger" @click="logoutUser(scope.row)">删除</el-button>

                  <el-select v-model="scope.row.value" @change="changeSelcet" size="mini" placeholder="选择公告操作">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <Pagecomponent
          :total="total"
          ref="pagecom"
          :pagesizelist="[15, 20]"
          @getAdminRative="pageChange"
        ></Pagecomponent>
      </div>
      <learder ref="leaderCom" :userRow="userRow"></learder>
    </div>
    <UsreMessageDialog
      ref="UsreMessageDialog"
      @getList="getUserRelease"
      :show.sync="showUserDialog"
      :editUserId="editUserId"
      :PnoticeType="noticeType"
      :Ptype="type"
    />
    <noticeDialog ref="noticeDialog"></noticeDialog>
  </div>
</template>
<script>
import { deletebsProBiddingApi } from '@/apiList/api_power'

import { editbsProBiddingApi } from '@/apiList/api_power'
import { uploadFile } from '@/apiList/api_file'
import Pagecomponent from '@/components/custom/PageCom.vue'
import { filterListTree, getTreeObjHanlder } from '@/utils/tools/funtctionAPI.js'
import learder from './components/learder.vue'
import UsreMessageDialog from './components/UsreMessageDialog.vue'
import authorityApi from '@/utils/tools/authorityApi/index.js'
import noticeDialog from '@/views/pages/invitation/components/noticeDilaog.vue'
import { local } from '@/utils/jcore/storage'

export default {
  name: 'user',
  components: {
    UsreMessageDialog,
    noticeDialog,
    Pagecomponent,
    learder
  },
  data() {
    return {
      userInfo: {
        sysRoleCodes: []
      },
      actionUrl: '',
      input: '', //修改密码输入框
      loading: false,
      editUserId: '',
      userIsEdit: false,
      showUserDialog: false,
      isEdstatusList: [
        {
          name: '未发布',
          value: 1
        },
        {
          name: '已发布',
          value: 0
        }
      ],
      logoutList: [
        {
          name: '已注销',
          value: 1
        },
        {
          name: '未注销',
          value: 0
        }
      ],
      rules: {
        psd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      },
      options: [],
      ruleForm: {
        psd: ''
      },
      stopFlag: '',
      deleteFlag: '',

      defaulitOpenList: [], //树形节点记录节点状态
      organizeId: '', //当前选中机构id
      dialogFormVisible: false,
      getUserList: [], // 获取到的树状结构数组
      // falg: true, //默认复选框隐藏
      // 表格数组
      tableDatarow: [],
      // 表单数组
      tableData: new Array(10).fill(),
      // 表头数据
      name: '机构名称', //名称
      sort: '序号', //编码
      formLabelWidth: '120px',

      // 上级机构级别
      optionslist: [],
      value1: '',
      degPageTypeName: '', //机构名称
      degPageTypeNameId: '', //机构ID
      degPageTypeLs: null,
      degPageTypeDutyId: '', //岗位ID
      dialogImageUrl: '',
      dialogVisible: false,
      currentPage: 1,
      total: 0, //总数
      pageSize: 15, //每页显示多少条
      tablerow: [], //当前选中行
      userName: '', //名称查询
      datefb: '', //名称查询
      linkUserName: '', //名称查询
      input4: '', //机构查询
      dutyName: '', //岗位查询
      PostList: [], // 岗位列表数据
      isShowFalg: false,
      userRow: null //当前所选用户信息
    }
  },
  created() {
    Object.assign(this.userInfo, local.get('userInfo'))
    this.userInfo.sysRoleCodes.forEach((item) => {
      if (item.indexOf('admin') != -1) {
        this.isShowFalg = true
      }
    })
    this.getUserRelease()
    this.creator = local.get('authorityList').id
    // this.dialogbtnhandle(true)
    this.noticeType = this.$route.meta.type
    this.type = this.$route.meta.fatherType
    console.log(this.$route.meta.type, 'noticeType')
    console.log(this.$route.meta.fatherType, 'type')
    this.handersChangeSelect() //初始化下拉选择
  },
  methods: {
    handersChangeSelect() {
      //当前是中标
      if (this.$route.meta.type == 0 && this.$route.meta.fatherType == 0) {
        this.options = [
          {
            value: 1,
            label: '发布变更公告'
          },
          {
            value: 2,
            label: '发布中标公告'
          },
          {
            value: 3,
            label: '发布废标公告'
          }
        ]
        return
      }
      if (this.$route.meta.type == 1 && this.$route.meta.fatherType == 0) {
        this.options = [
          {
            value: 1,
            label: '发布变更公告'
          }
        ]
        return
      }
      if (this.$route.meta.type == 1 && this.$route.meta.fatherType == 0) {
        this.options = [
          {
            value: 1,
            label: '发布变更公告'
          }
        ]
        return
      }
      if (this.$route.meta.type == 3 && this.$route.meta.fatherType == 0) {
        this.options = [
          {
            value: 1,
            label: '发布变更公告'
          }
        ]
        return
      }
    },
    changeSelcet(val) {
      switch (val) {
        case 1:
          // 变更公告
          this.noticeType = 2
          this.type = 0
          this.showUserDialog = true
          break
        case 2:
          // 中标公告
          this.noticeType = 1
          this.type = 0
          this.showUserDialog = true
          break
        case 3:
          // 中标公告
          this.noticeType = 3
          this.type = 0
          this.showUserDialog = true
          break

        default:
          break
      }
    },
    openNoticeDialog(row) {
      this.$refs.noticeDialog.formdata = row
      this.$refs.noticeDialog.drawer = true

      switch (row.noticeType) {
        case 0:
          this.$refs.noticeDialog.text = '招标/采购公告' || ''
          break

        case 1:
          this.$refs.noticeDialog.text = '成交/中标公告' || ''
          break

        case 2:
          this.$refs.noticeDialog.text = '补遗/澄清/更正公告' || ''
          break

        case 3:
          this.$refs.noticeDialog.text = '废标/终止公告' || ''
          break

        case 4:
          this.$refs.noticeDialog.text = '履约/合同公告' || ''
          break

        default:
          break
      }
    },
    selectLearder(row) {
      this.userRow = row
      this.$refs.leaderCom.drawer = true
    },

    clearpsd() {
      this.ruleForm.psd = ''
      this.$refs.ruleForm.clearValidate()
    },

    formatter(row) {
      let str = ''

      // <!-- 0企业 1政府机构 2事业单位 3社会团体 4其他 -->
      switch (row.noticeType) {
        case 0:
          return '招标/采购公告'

        case 1:
          return '成交/中标公告'

        case 2:
          return '补遗/澄清/更正公告'

        case 3:
          return '废标/终止公告'

        case 4:
          return '履约/合同公告'
      }
    },
    sysRoleNamesFormatter(row) {
      if (!row.sysRoleNames) {
        return ''
      } else {
        return row.sysRoleNames.join(',')
      }
    },
    selectable(row) {
      if (row.id !== authorityApi.sysOverallInfo.overallUserID) {
        return true
      } else {
        return false
      }
    },

    handleClears() {
      this.input4 = ''
      this.degPageTypeName = ''
      this.degPageTypeNameId = ''
      this.degPageTypeDutyId = ''
    },
    clearDutyId() {
      this.degPageTypeDutyId = ''
    },
    pageChange(list) {
      console.log(list)
      this.getUserRelease(list)
    },
    // 获取页面数据
    getUserRelease(list) {
      if (list && list.length > 0) {
        this.pageSize = list[0]
        this.currentPage = list[1]
      }
      this.loading = true
      let data = {
        status: this.linkUserName,
        noticeTitle: this.userName || '',
        noticeType: this.$route.meta.type == 5 ? '' : this.$route.meta.type,
        type: this.$route.meta.fatherType,
        creator: authorityApi.sysOverallInfo.id,
        pageSize: this.pageSize || list[0],
        currentPage: this.currentPage || list[1]
      }
      if (this.datefb) {
        data.startTime = this.datefb[0]
        data.endTime = this.datefb[1]
      }
      authorityApi.bsProBiddingApi.page(data, (res) => {
        this.loading = false
        if (res.records === undefined) {
          this.tableDatarow = []
          this.total = 0
        } else {
          this.tableDatarow = res.records
          this.total = res.total
        }
      })
    },

    //新建用户
    addUser() {
      this.editUserId = ''
      this.noticeType = this.$route.meta.type
      this.type = this.$route.meta.fatherType
      this.showUserDialog = true
    },
    //编辑用户
    updateUser(row) {
      this.editUserId = row.id
      this.noticeType = this.$route.meta.type
      this.type = this.$route.meta.fatherType
      this.showUserDialog = true
    },
    async logoutUser(row) {
      //删除
      await deletebsProBiddingApi({ id: row.id })

      this.getUserRelease()
    },
    //刷新按钮
    RefreshUser() {
      this.getUserRelease()
    },
    downloadFile() {
      let eleLink = document.createElement('a')
      eleLink.download = 'fileName'
      eleLink.style.display = 'none'
      let url = ''
      // 字符内容转变成blob地址
      // if (process.env.NODE_ENV === 'development') {
      //   url = 'http://124.71.132.153:18001'
      // } else {
      //   url = window.origin
      // }
      eleLink.href = 'http://124.71.132.153/file/oss/downloadFile?fid=1;474f793360'
      // 自动触发点击
      document.body.appendChild(eleLink)
      eleLink.click()
      // 然后移除
      document.body.removeChild(eleLink)

      // http://47.109.19.5:18001/oss/downloadFile?fid=1;f1b92595bb
      // <a :href="item?.url" class="aBlink">{{ item.name }}</a>
    },
    async selectFileChange(file) {
      const f = new FormData()
      f.append('file', file.raw)
      f.append('creator', local.get('authorityList').id)
      const res = await uploadFile(f)
      const fid = res.fileInfos[0].fid

      authorityApi.sysImportApi.importFileApi(
        { fid: fid, noticeType: this.$route.meta.type, type: this.$route.meta.fatherType },
        (res) => {
          this.getUserRelease()
        }
      )
    },

    //当前行选中时变化
    tablerowchange(row) {
      this.tablerow = row
    },
    //开关是否发布
    async changeHandler(row) {
      if (row.status) {
        await editbsProBiddingApi({ id: row.id, status: 1 })
        this.getUserRelease()
      } else {
        await editbsProBiddingApi({ id: row.id, status: 0 })
        this.getUserRelease()
      }
    },
    //搜索用户事件
    serachResult() {
      this.getUserRelease()
    },
    resetResult() {
      this.linkUserName = ''
      this.userName = ''
      this.organizationName = ''
      this.getUserRelease()
    }
  },
  watch: {
    input4(newval, oldval) {
      if (!newval) {
        this.dutyName = ''
      }
    },
    organizeId(newval) {
      if (!newval) {
        this.PostList = []
      }
    }
  }
}
</script>
<style lang="less" scoped>
.RootBox {
  background-color: #fff;
  height: 100%;
}
.iconlist {
  align-items: center;
  display: flex;
  margin: 24px 0px;
}
.mbnav {
  background-color: #fff;
  .mbnavson {
    height: 51px;
    margin-left: 24px;
    line-height: 51px;
    border-bottom: 1px solid #4285f4;
  }
  .mbnavsontwo {
    margin-left: 24px;
    line-height: 64px;
    display: flex;
    align-items: center;
  }
  .mobile {
    line-height: 21px;
    margin-right: 25px;
  }
  .el-button--mini {
    font-size: 12px;

    // width: 72px;
  }
}
/deep/ .el-breadcrumb__separator[class*='icon'] {
  color: #4285f4;
}
/deep/.el-breadcrumb__inner {
  color: #4285f4 !important;
  font-size: 14px;
}
.tabbleList {
  background-color: #fff;
  height: 100%;
  display: flex;
}
/deep/.el-tree {
  height: 100%;
  width: 354px;
}
/deep/.el-table__row {
  height: 45px;
}
/deep/.el-tree-node__content {
  height: 45px;
  // background-color: red;
}
/deep/.el-tree-node__content:nth-child(add) {
  background-color: red;
}
/deep/.fatheritemlistname > .el-table--scrollable-x .el-table__body-wrapper {
  overflow-x: hidden !important;
}
.itemlistname {
  width: 100%;
  height: 45px;
  position: relative;
  // z-index: 2;
}
.fatheritemlistname {
  border-top: 1px solid #ccc !important;
  background-color: #fff !important;
  width: 354px !important;
  position: relative;
  display: flex;
  padding-top: 3.5px;
  padding-right: 20px;
}
.fatherform {
  width: 100%;
  padding-left: 20px;
  background-color: #fff !important;
}
/deep/.el-table__body {
  width: none !important;
}
/deep/ .el-table__header {
  width: none;
}
/deep/.cell {
  text-align: center;
}
/deep/ .el-dialog__title {
  color: #4285f4;
}
/deep/ .el-dialog {
  width: 75%;
}
/deep/ .el-dialog__header {
  border-bottom: 1px solid #4285f4;
}
/deep/.el-breadcrumb__inner {
  color: #4285f4 !important;
  font-size: 14px;
}
/deep/.el-breadcrumb__inner.is-link {
  color: #4285f4 !important;
  font-size: 14px !important ;
}
.dialog-footer {
  text-align: center;
}
.dialog-footer {
  text-align: center;
}

// /deep/ .el-input__inner {
//   height: 33px;
// }
/deep/ .el-card {
  // width: 40%;
  box-shadow: none !important;
}
.carld {
  width: 100%;
  display: flex;
  justify-content: center;
}
.formbtn {
  display: flex;
  height: 100%;
  margin: 0 30px;
  justify-content: center;
  flex-direction: column;
}
.gangwei {
  margin: 0 0 30px 0 !important;
}
.gangweistyle {
  color: #4285f4;
  font-size: 20px;
  text-align: center;
  font-weight: bold;
}
/deep/.clearfix {
  text-align: center;
}

.divbox {
  margin-top: 10px;
}
/deep/.is-leaf {
  background-color: none;
}
/deep/.el-table thead {
  background-color: #f6f6f6;
}
.search {
  margin-left: 24px;
}
.searchfather {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
.input-with-select {
  margin-left: 30px !important;
}
/deep/.el-pagination {
  margin-top: 20px;
  text-align: center;
}
/deep/ .el-pagination .el-pagination__jump .el-input {
  width: 50px !important;
}
/deep/.el-pagination {
  margin-bottom: 20px;
}
/deep/.el-select {
  margin-left: 30px;
}
/deep/.input-with-select {
  margin: 0;
}
/deep/.el-select .el-input .el-select__caret.is-reverse {
  transform: rotateZ(-180deg) translateY(-4px);
}
/deep/.el-select .el-input .el-select__caret {
  color: #c0c4cc;
  font-size: 14px;
  transition: transform 0.3s;
  transform: rotateZ(-90deg);
  /* transform: translateY(-4px); */
  cursor: pointer;
}
/deep/.el-tree .el-tree-node__children .el-tree-node__content {
  background-color: transparent !important;
}
.btnicon {
  font-size: 12px;
}
.addUser {
  background-color: #4285f4;
  color: #fff;
}
.updateUser {
  background-color: #1c84c6;
  color: #fff;
}
.Refresh {
  background-color: #f7a54a;
  color: #fff;
}
.el-select {
  width: 100%;
}
.el-date-editor {
  width: 100%;
  margin-left: 20px;
}
.linkTitle {
  color: #4285f4;
  text-decoration: underline;
  cursor: pointer;
}
</style>
